<script setup lang="ts">
import { ref, reactive, onMounted, computed } from 'vue'
import { useAuthStore } from '../../store/useAuthStore'
import { ElMessage } from 'element-plus'
import { User, Message, Phone, Camera, PieChart, Document, Timer, Promotion } from '@element-plus/icons-vue'
import type { UserInfo } from '../../types/auth'
import { useRouter } from 'vue-router'

const authStore = useAuthStore()
const router = useRouter()

// 用户信息
const userInfo = reactive<Partial<UserInfo>>({
  username: authStore.user?.username || '',
  email: authStore.user?.email || '',
  avatar: authStore.user?.avatar || '',
  avatarUrl: authStore.user?.avatarUrl || '',
  nickname: authStore.user?.nickname || '',
  phone: authStore.user?.phone || '',
  role: authStore.user?.role || ''
})

// 头像URL计算属性
const displayAvatarUrl = computed(() => {
  return userInfo.avatarUrl || userInfo.avatar || ''
})

// 用户统计数据（模拟）
const userStats = reactive({
  totalProcessed: 86,
  processingQueue: 3,
  savedWatermarks: 12,
  averageTime: '2.5秒'
})

// 近期活动（模拟）
const recentActivities = ref([
  {
    id: 1,
    type: '处理完成',
    title: '图片批量水印添加',
    date: '2024-04-20 09:30',
    count: 36
  },
  {
    id: 2,
    type: '处理完成',
    title: '水印效果优化',
    date: '2024-04-18 15:45',
    count: 5
  },
  {
    id: 3,
    type: '水印保存',
    title: '新建公司Logo水印',
    date: '2024-04-15 11:20',
    count: 1
  }
])

// 跳转到账号设置
const goToSettings = () => {
  router.push('/account/settings')
}

// 加载用户信息
onMounted(() => {
  // 强制重新加载用户信息，确保数据是最新的
  authStore.getCurrentUser().then(() => {
    // 更新本地表单数据
    Object.assign(userInfo, authStore.user)
  })
})
</script>

<template>
  <div class="profile-container">
    <div class="profile-header">
      <div class="header-bg-shapes">
        <div class="shape shape-1"></div>
        <div class="shape shape-2"></div>
        <div class="shape shape-3"></div>
      </div>
      <div class="header-content">
        <h1 class="page-title">个人中心</h1>
        <p class="page-desc">查看您的账号摘要信息和使用统计</p>
      </div>
      <div class="header-avatar">
        <div class="avatar-frame">
          <el-avatar :size="110" :src="displayAvatarUrl">
            {{ userInfo.username?.charAt(0).toUpperCase() }}
          </el-avatar>
        </div>
        <div class="user-name">{{ userInfo.nickname || userInfo.username }}</div>
        <div class="user-role" :class="{'is-admin': userInfo.role === 'ADMIN'}">
          {{ userInfo.role === 'ADMIN' ? '管理员' : '用户' }}
        </div>
      </div>
    </div>
    
    <div class="profile-body">
      <!-- 用户信息卡片 -->
      <el-row :gutter="20">
        <el-col :span="24" :lg="8">
          <el-card class="user-info-card hover-lift" shadow="hover">
            <div class="card-header">
              <h2 class="card-title">基本信息</h2>
              <el-button type="primary" link @click="goToSettings" class="settings-link">
                设置
                <el-icon class="el-icon--right"><User /></el-icon>
              </el-button>
                </div>
            
            <div class="info-list">
              <div class="info-item">
                <div class="info-label">
                  <el-icon><User /></el-icon>
                  <span>用户名</span>
                </div>
                <div class="info-value">{{ userInfo.username }}</div>
              </div>
              
              <div class="info-item">
                <div class="info-label">
                  <el-icon><Message /></el-icon>
                  <span>邮箱</span>
                </div>
                <div class="info-value">{{ userInfo.email || '未设置' }}</div>
              </div>
              
              <div class="info-item">
                <div class="info-label">
                  <el-icon><Phone /></el-icon>
                  <span>手机号</span>
                </div>
                <div class="info-value">{{ userInfo.phone || '未设置' }}</div>
              </div>
              
              <div class="info-item">
                <div class="info-label">
                  <el-icon><User /></el-icon>
                  <span>角色</span>
                </div>
                <div class="info-value">{{ userInfo.role === 'ADMIN' ? '管理员' : '普通用户' }}</div>
              </div>
            </div>
          </el-card>
        </el-col>
        
        <!-- 统计信息卡片 -->
        <el-col :span="24" :lg="16">
          <el-card class="stats-card hover-lift" shadow="hover">
            <div class="card-header">
              <h2 class="card-title">使用统计</h2>
            </div>
            
            <div class="stats-grid">
              <div class="stat-item">
                <div class="stat-icon">
                  <el-icon><Document /></el-icon>
                </div>
                <div class="stat-data">
                  <div class="stat-value">{{ userStats.totalProcessed }}</div>
                  <div class="stat-label">已处理图片</div>
                </div>
              </div>
              
              <div class="stat-item">
                <div class="stat-icon">
                  <el-icon><Timer /></el-icon>
                </div>
                <div class="stat-data">
                  <div class="stat-value">{{ userStats.processingQueue }}</div>
                  <div class="stat-label">待处理队列</div>
                </div>
              </div>
              
              <div class="stat-item">
                <div class="stat-icon">
                  <el-icon><Promotion /></el-icon>
                </div>
                <div class="stat-data">
                  <div class="stat-value">{{ userStats.savedWatermarks }}</div>
                  <div class="stat-label">保存的水印</div>
                </div>
              </div>
              
              <div class="stat-item">
                <div class="stat-icon">
                  <el-icon><PieChart /></el-icon>
                </div>
                <div class="stat-data">
                  <div class="stat-value">{{ userStats.averageTime }}</div>
                  <div class="stat-label">平均处理时间</div>
                </div>
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>
      
      <!-- 近期活动 -->
      <el-card class="activity-card hover-lift" shadow="hover">
        <div class="card-header">
          <h2 class="card-title">近期活动</h2>
          </div>

        <div class="timeline">
          <div v-for="activity in recentActivities" :key="activity.id" class="timeline-item">
            <div class="timeline-icon" :class="{ 
              'type-process': activity.type === '处理完成',
              'type-save': activity.type === '水印保存'
            }">
              <el-icon v-if="activity.type === '处理完成'"><Document /></el-icon>
              <el-icon v-else-if="activity.type === '水印保存'"><Promotion /></el-icon>
            </div>
            <div class="timeline-content">
              <div class="timeline-date">{{ activity.date }}</div>
              <div class="timeline-title">{{ activity.title }}</div>
              <div class="timeline-detail">
                <el-tag size="small" :type="activity.type === '处理完成' ? 'success' : 'primary'">
                  {{ activity.type }}
                </el-tag>
                <span class="count">{{ activity.count }} 张图片</span>
              </div>
            </div>
          </div>
          
          <div class="timeline-end">
            <el-button link type="primary">查看更多活动</el-button>
          </div>
        </div>
    </el-card>
    </div>
  </div>
</template>

<style scoped>
.profile-container {
  padding: 20px;
  max-width: 1100px;
  margin: 0 auto;
  color: #333;
}

.profile-header {
  position: relative;
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 30px;
  background: linear-gradient(135deg, #3a8ee6, #5254ae);
  border-radius: 16px;
  padding: 40px;
  color: #fff;
  box-shadow: 0 10px 30px rgba(58, 142, 230, 0.2);
  overflow: hidden;
}

.header-bg-shapes {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 0;
  opacity: 0.15;
  overflow: hidden;
}

.shape {
  position: absolute;
  border-radius: 50%;
}

.shape-1 {
  top: -50px;
  right: -50px;
  width: 200px;
  height: 200px;
  background: linear-gradient(to right, #ffffff, rgba(255, 255, 255, 0.2));
}

.shape-2 {
  bottom: -80px;
  left: 10%;
  width: 180px;
  height: 180px;
  background: linear-gradient(to right, #ffffff, rgba(255, 255, 255, 0.1));
}

.shape-3 {
  top: 30%;
  left: 30%;
  width: 80px;
  height: 80px;
  background: linear-gradient(to right, #ffffff, rgba(255, 255, 255, 0.3));
}

.header-content {
  flex: 1;
  position: relative;
  z-index: 1;
}

.page-title {
  font-size: 32px;
  font-weight: 700;
  margin: 0 0 12px 0;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.page-desc {
  font-size: 16px;
  opacity: 0.9;
  margin: 0;
  max-width: 400px;
}

.header-avatar {
  position: relative;
  z-index: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.avatar-frame {
  padding: 5px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 50%;
  margin-bottom: 5px;
}

.user-name {
  margin-top: 15px;
  font-size: 20px;
  font-weight: 600;
}

.user-role {
  margin-top: 8px;
  background: rgba(255, 255, 255, 0.2);
  padding: 4px 15px;
  border-radius: 20px;
  font-size: 13px;
  font-weight: 500;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
}

.user-role:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.user-role.is-admin {
  background: rgba(255, 70, 70, 0.3);
}

.profile-body {
  position: relative;
}

.hover-lift {
  transition: all 0.3s ease;
}

.hover-lift:hover {
  transform: translateY(-4px);
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.08);
}

/* 卡片样式 */
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  padding-bottom: 15px;
  border-bottom: 1px solid #f0f0f0;
}

.card-title {
  font-size: 18px;
  font-weight: 600;
  margin: 0;
  color: #303133;
  display: flex;
  align-items: center;
}

.card-title:before {
  content: '';
  display: inline-block;
  width: 4px;
  height: 18px;
  background: linear-gradient(to bottom, #3a8ee6, #5254ae);
  margin-right: 10px;
  border-radius: 2px;
}

.settings-link {
  font-size: 14px;
}

/* 用户信息卡片 */
.user-info-card {
  border-radius: 12px;
  margin-bottom: 20px;
  overflow: hidden;
}

.info-list {
  padding: 0 10px;
}

.info-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 0;
  border-bottom: 1px dashed #f5f5f5;
}

.info-item:last-child {
  border-bottom: none;
}

.info-label {
  color: #606266;
  font-weight: 500;
  display: flex;
  align-items: center;
}

.info-label .el-icon {
  margin-right: 8px;
  color: #5254ae;
}

.info-value {
  font-weight: 500;
  color: #303133;
}

/* 统计卡片 */
.stats-card {
  border-radius: 12px;
  margin-bottom: 20px;
  overflow: hidden;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  grid-gap: 20px;
}

.stat-item {
  padding: 20px;
  background: #f9fafb;
  border-radius: 10px;
  display: flex;
  align-items: center;
  transition: all 0.3s;
}

.stat-item:hover {
  background: #f2f6fc;
  transform: translateY(-2px);
}

.stat-icon {
  width: 50px;
  height: 50px;
  border-radius: 12px;
  background: linear-gradient(135deg, #3a8ee6, #5254ae);
  display: flex;
  justify-content: center;
  align-items: center;
  margin-right: 15px;
  color: white;
  font-size: 24px;
  box-shadow: 0 4px 10px rgba(58, 142, 230, 0.2);
}

.stat-data {
  flex: 1;
}

.stat-value {
  font-size: 24px;
  font-weight: 700;
  color: #303133;
  margin-bottom: 3px;
}

.stat-label {
  font-size: 14px;
  color: #909399;
}

/* 活动卡片 */
.activity-card {
  border-radius: 12px;
  overflow: hidden;
}

.timeline {
  position: relative;
  padding: 20px 10px;
}

.timeline:before {
  content: '';
  position: absolute;
  top: 0;
  bottom: 0;
  left: 26px;
  width: 2px;
  background: #f0f0f0;
}

.timeline-item {
  position: relative;
  padding-bottom: 30px;
  padding-left: 60px;
}

.timeline-icon {
  position: absolute;
  top: 0;
  left: 17px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: #409EFF;
  display: flex;
  justify-content: center;
  align-items: center;
  color: white;
  font-size: 12px;
  box-shadow: 0 0 0 4px white;
  z-index: 1;
}

.timeline-icon.type-process {
  background: #67c23a;
}

.timeline-icon.type-save {
  background: #409EFF;
}

.timeline-content {
  background: #f9fafb;
  border-radius: 10px;
  padding: 15px;
  transition: all 0.3s;
}

.timeline-content:hover {
  background: #f2f6fc;
  transform: translateY(-2px);
}

.timeline-date {
  font-size: 13px;
  color: #909399;
  margin-bottom: 8px;
}

.timeline-title {
  font-size: 16px;
  font-weight: 600;
  color: #303133;
  margin-bottom: 10px;
}

.timeline-detail {
  display: flex;
  align-items: center;
}

.count {
  margin-left: 10px;
  font-size: 13px;
  color: #606266;
}

.timeline-end {
  padding-left: 60px;
  margin-top: 10px;
  text-align: center;
}

@media (max-width: 768px) {
  .profile-header {
    flex-direction: column;
    text-align: center;
    padding: 25px 20px;
  }
  
  .header-content {
    margin-bottom: 25px;
  }
  
  .avatar-frame {
    margin: 0 auto;
  }
  
  .stats-grid {
    grid-template-columns: 1fr;
  }
}

@media (min-width: 768px) and (max-width: 991px) {
  .stats-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}

@media (min-width: 992px) {
  .stats-grid {
    grid-template-columns: repeat(4, 1fr);
  }
}
</style> 